package com.turman.coroutionsample.view

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.turman.coroutionsample.bus.LocalEventBus
import com.turman.coroutionsample.ui.theme.CoroutionSampleTheme
import com.turman.coroutionsample.viewmodel.SharedFlowViewModel

@Composable
fun SharedFlowPage() {
    val viewModel = viewModel<SharedFlowViewModel>()
    val number1 by LocalEventBus.events.collectAsState(null)
    val number2 by LocalEventBus.events.collectAsState(null)
    val number3 by LocalEventBus.events.collectAsState(null)
    CoroutionSampleTheme {
        Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
            Column(horizontalAlignment = Alignment.CenterHorizontally) {
                Spacer(modifier = Modifier.height(20.dp))
                Text(text = "${number1?.timestamp}")
                Spacer(modifier = Modifier.height(20.dp))
                Text(text = "${number2?.timestamp}")
                Spacer(modifier = Modifier.height(20.dp))
                Text(text = "${number3?.timestamp}")
                Spacer(modifier = Modifier.height(20.dp))

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceEvenly
                ) {
                    Button(onClick = { viewModel.startRefresh() }) {
                        Text(text = "start")
                    }
                    Button(onClick = { viewModel.stopRefresh() }) {
                        Text(text = "stop")
                    }
                }
            }
        }
    }
}